# -*- coding: utf-8 -*-
#!/usr/bin/python
################################################
# NSFOCUS Corporation. All rights Reserved
################################################
# @Date : 2023-11-08
# @Author : zhu
# @File : shubiao.py
# @Software: VS
# @Desc: 鼠标点击
import tkinter as tk
from tkinter import messagebox
import pyautogui
import threading
import time

# 全局变量
clicking = False
x, y = 0, 0

# 获取鼠标位置

def get_mouse_position():
    global x, y
    x, y = pyautogui.position()
    pos_label.config(text=f"位置: ({x}, {y})")

# 鼠标点选位置

def select_mouse_position():
    messagebox.showinfo("提示", "请在 5 秒内移动鼠标到要选择的位置，并等待位置获取...")
    time.sleep(5)
    get_mouse_position()

# 自动点击功能

def auto_click():
    global clicking
    try:
        interval = float(interval_entry.get())
        while clicking:
            pyautogui.click(x, y)  # 在指定位置点击
            time.sleep(interval)  # 等待设定的时间间隔
    except ValueError:
        messagebox.showerror("错误", "请输入有效的时间间隔！")

# 开始点击

def start_clicking():
    global clicking
    if not clicking:
        clicking = True
        t = threading.Thread(target=auto_click)  # 使用线程避免 GUI 阻塞
        t.start()

# 停止点击

def stop_clicking():
    global clicking
    clicking = False

# 取消选择

def cancel_selection():
    global x, y
    x, y = 0, 0
    pos_label.config(text="位置: (0, 0)")

# 创建 GUI 界面
root = tk.Tk()
root.title("鼠标自动点击器")

# 显示鼠标坐标
pos_label = tk.Label(root, text="位置: (0, 0)")
pos_label.grid(row=0, column=0, padx=10, pady=10)

# 获取鼠标位置按钮
get_pos_button = tk.Button(root, text="点选位置", command=select_mouse_position)
get_pos_button.grid(row=0, column=1, padx=10, pady=10)

# 取消按钮
cancel_button = tk.Button(root, text="取消", command=cancel_selection)
cancel_button.grid(row=0, column=2, padx=10, pady=10)

# 时间间隔输入框
tk.Label(root, text="时间间隔(秒):").grid(row=1, column=0, padx=10, pady=10)
interval_entry = tk.Entry(root)
interval_entry.grid(row=1, column=1, padx=10, pady=10)
interval_entry.insert(0, "1")  # 默认1秒

# 控制按钮
start_button = tk.Button(root, text="开始", command=start_clicking, bg="green", fg="white")
start_button.grid(row=2, column=0, padx=10, pady=10)

stop_button = tk.Button(root, text="停止", command=stop_clicking, bg="red", fg="white")
stop_button.grid(row=2, column=1, padx=10, pady=10)

# 主循环
root.mainloop()
